package com.fanbai.score.user.http;

import android.content.Context;

import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.http.HttpUtils;
import com.fanbai.score.common.http.WebCodes;
import com.fanbai.score.user.bean.ProductInfo_Bean;
import com.fanbai.score.user.bean.ShopClass_Bean;
import com.fanbai.score.user.bean.ShopInfo_Bean;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import static com.fanbai.score.common.Utils.JsonUtils.toArr;

/**
 * Created by acer on 2017/6/3.
 * 顾客版产品相关
 */

public class UserProduct_RequestNet_Utils {
    /**
     * 获取分类信息
     * @param ctx
     * @param callBack
     */
    public static void getClassInfos(final Context ctx, final CallBack<List<ShopClass_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getclassinfos");
        String url = WebCodes.HTTP_URL_USER_PRODUCT ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<ShopClass_Bean> shopClassBeen = JsonUtils.toArr(body, ShopClass_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, shopClassBeen);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取兑换产品
     * @param ctx
     * @param callBack
     */
    public static void getProductInfos(final Context ctx, final CallBack<List<ProductInfo_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getproductinfos");
        String url = WebCodes.HTTP_URL_USER_PRODUCT ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<ProductInfo_Bean> shopClassBeen = JsonUtils.toArr(body, ProductInfo_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, shopClassBeen);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取分类的商户信息
     * @param ctx
     * @param cid
     * @param callBack
     *
     * 1、获取这个接口后，直接调用http://score.xinyou777.cn/customer/shop/shopinfo.php?sid=%ld
    这个是网页版
    2、如果有两个sinfos，读取第一个sinfo
     */
    public static void getShopInfosByCid(final Context ctx, String cid, final CallBack<List<ShopInfo_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getshopinfosbycid");
        params.put("cid", cid);
        String url = WebCodes.HTTP_URL_USER_SHOP ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<ShopInfo_Bean> beans = JsonUtils.toArr(body, ShopInfo_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, beans);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取兑换产品详情
     * @param ctx
     * @param pid
     * @param callBack
     */
    public static void getProductInfoByPid(final Context ctx, String pid, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getproductinfobypid");
        params.put("pid", pid);
        String url = WebCodes.HTTP_URL_USER_PRODUCT ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取分类详情
     * @param ctx
     * @param cid
     * @param callBack
     */
    public static void getClassInfoByCid(final Context ctx, String cid, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getclassinfobycid");
        params.put("cid", cid);
        String url = WebCodes.HTTP_URL_USER_PRODUCT ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }
}
